--
-- DESCRIPTION
--
-- @COMPANY **
-- @AUTHOR **
-- @DATE ${date} ${time}
--

---@type GM_Duel_C
local M = UnLua.Class()

--- 屏幕
local Screen = require("Screen")

local KismetSystemLibrary = UE.UKismetSystemLibrary
local Math = UE.UKismetMathLibrary
local GameplayStatics = UE.UGameplayStatics

local BP_ExclamationClass = UE.UClass.Load("/Script/Engine.Blueprint'/QuickDraw/Blueprints/BP_Exclamation.BP_Exclamation_C'")

local SamuraiPlayerClass = UE.UClass.Load("/Script/Engine.Blueprint'/QuickDraw/Blueprints/BP_Samurai_Player.BP_Samurai_Player_C'")
local SamuraiNPCClass = UE.UClass.Load("/Script/Engine.Blueprint'/QuickDraw/Blueprints/BP_Samurai_NPC.BP_Samurai_NPC_C'")

--- 开始攻击阶段
local function StartDraw(self)
    Screen.Print(string.format("%s：启动！！", self:EnumToString(self.Phase)))
    KismetSystemLibrary.Delay(self, Math.RandomFloatInRange(self.MinDrawDelay, self.MaxDrawDelay))

    self.Phase = UE.EPhase.EP_Draw
    Screen.Print(string.format("%s：结束！！", self:EnumToString(self.Phase)))
    self.DrawPhaseStarted:Broadcast()

    if self.ExclamationRef then
        self.ExclamationRef:SetVisibility(true)
    else
        Screen.Print("ExclamationRef 无效！！！")
    end
end

--- 延迟调用攻击阶段
function M:DelayStartDraw()
    coroutine.resume(coroutine.create(StartDraw), self)
end

-- function M:Initialize(Initializer)
-- end

-- function M:UserConstructionScript()
-- end

function M:ReceiveBeginPlay()
    if BP_ExclamationClass then
        self.ExclamationRef = GameplayStatics.GetActorOfClass(self, BP_ExclamationClass)
    end

    if SamuraiPlayerClass then
        self.SamuraiPlayer = GameplayStatics.GetActorOfClass(self, SamuraiPlayerClass)
    end
    if SamuraiNPCClass then
        self.SamuraiNPC = GameplayStatics.GetActorOfClass(self, SamuraiNPCClass)
    end

end

-- function M:ReceiveEndPlay()
-- end

function M:ReceiveTick(DeltaSeconds)

    self.ElapsedTime = self.ElapsedTime + DeltaSeconds

    if self.Phase == UE.EPhase.EP_Intro and self.ElapsedTime >= self.RoundStartTime then
        self.Phase = UE.EPhase.EP_Wait
        self:DelayStartDraw()
        --else
        --    UE.UKismetSystemLibrary.PrintString(
        --            self,
        --            string.format("%s", self:EnumToString(self.Phase)),
        --            true,
        --            true,
        --            UE.FLinearColor(1, 0, 0, 1),
        --            1,
        --            "logx"
        --    )
    end

end

-- function M:ReceiveAnyDamage(Damage, DamageType, InstigatedBy, DamageCauser)
-- end

-- function M:ReceiveActorBeginOverlap(OtherActor)
-- end

-- function M:ReceiveActorEndOverlap(OtherActor)
-- end

--- 枚举转换
function M:EnumToString(EnumValue)
    if EnumValue == UE.EPhase.EP_Intro then
        return "Intro       -   准备阶段"
    elseif EnumValue == UE.EPhase.EP_Wait then
        return "Wait        -   等待阶段"
    elseif EnumValue == UE.EPhase.EP_Draw then
        return "Draw        -   进攻阶段"
    elseif EnumValue == UE.EPhase.EP_Finished then
        return "Finished    -   完成阶段"
    end
end

--- 设置游戏结束
function M:SetGameFinished()
    self.Phase = UE.EPhase.EP_Finished
end

--- 攻击成功结束
function M:AttackSuccessful(IsPlayer)
    if IsPlayer then
        self.SamuraiNPC:SetCrossVisibility(false)
        self.SamuraiNPC:Defeated()
    else
        self.SamuraiPlayer:SetCrossVisibility(false)
        self.SamuraiPlayer:Defeated()
    end
    self:GameOver()
end

--- 游戏重置
function M:Reset()
    self.Phase = UE.EPhase.EP_Intro
    self.ElapsedTime = 0
    self.SamuraiPlayer:Reset()
    self.SamuraiNPC:Reset()
end

--- 开始延迟攻击
--local function DelayReset(self)
--
--    KismetSystemLibrary.Delay(self, 3)
--
--    self:Reset()
--end

--- 游戏结束
function M:GameOver()
    --- 蓝图里面的Delay用法
    -- coroutine.resume(coroutine.create(DelayReset), self)

    -- print("开始延迟！！！") 蓝图里面的(setTimer by Event)
    KismetSystemLibrary.K2_SetTimerDelegate({ self, self.Reset }, 3, false)
end

return M
